package com.sx.manage.controller.Product;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.sx.common.util.json.JackJsonUtil;
import com.sx.manage.controller.base.MBaseController;
import com.sx.manage.service.product.IMProductCallChargeService;
import com.sx.manage.service.sys.IMBaseSystemConfigInfoService;
import com.sx.manage.service.sys.IMSysProvinceService;
import com.sx.manage.servlet.ViewAndModel;
import com.sx.manage.util.ExcelUtils;
import com.sx.manage.util.PropertiesUtils;
import com.sx.model.constants.MSystemConstants;
import com.sx.model.manage.mybatis.form.product.MProductCallChargeInfoForm;
import com.sx.model.manage.mybatis.form.sys.MSystemConfigInfoForm;
import com.sx.model.manage.mybatis.vo.product.MProductCallChargeVO;
import com.sx.model.manage.mybatis.vo.product.MSetCallChargeVO;
import com.sx.model.manage.mybatis.vo.product.MStatusProductVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultExcelVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;
import com.sx.model.manage.mybatis.vo.sys.MProvinceVO;
import com.sx.model.manage.mybatis.vo.sys.MSysConfigVO;

/**
 * 话费充值
 * 
 * @author zwm
 *
 */
@Controller
@RequestMapping("/callchargeinfo")
@SuppressWarnings({ "rawtypes", "unchecked" })
public class MProductCallChargeController extends MBaseController {

	private Logger log = LoggerFactory.getLogger(this.getClass());
	@Resource(name = "productCallChargeServiceImpl")
	private IMProductCallChargeService productCallChargeServiceImpl;
	@Resource(name = "mSysProvinceServiceImpl")
	private IMSysProvinceService mSysProvinceServiceImpl;
	/** 系统参数配置 **/
	@Resource(name = "mBaseSystemConfigInfoServiceImpl")
	private IMBaseSystemConfigInfoService systemConfigInfoServiceImpl;

	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "callcharge_view_index")
	public ViewAndModel add_viewCallChargeInfo() {
		ViewAndModel model = new ViewAndModel(request, cache);
		model.setViewName("productsMgr/phoneRecharge/phoneRecharge");// 修改ftl页面名称
		ResultListVO<MProvinceVO> lvo = mSysProvinceServiceImpl.querySysProvince(null);
		//增加全国选项
		List<MProvinceVO> provinceList = new ArrayList<>();
		MProductCallChargeVO vo = new MProductCallChargeVO();
		MProvinceVO provinceVO = new MProvinceVO();
		provinceVO.setProvince_name("全国");
		provinceList.add(provinceVO);
		provinceList.addAll(lvo.getData());
		//传入界面
		vo.setProvince_list(provinceList);
		model.addObject("vo", vo);
		return model;
	}

	@RequestMapping("/callcharge_page")
	@ResponseBody
	public ResultListVO<MProductCallChargeVO> searchProductCallChargeInfo(MProductCallChargeInfoForm form) {
		return this.productCallChargeServiceImpl.queryCallCharge(form);
	}

	@RequestMapping("/callcharge_import")
	@ResponseBody
	public ResultListVO import_callChargeInfo(@RequestParam(value = "files", required = false) MultipartFile file) {
		if (null == file) {
			return new ResultListVO<>("sj.m.file.info.error");
		}
		String fileName = file.getOriginalFilename();
		log.info(" 公司用户=====导入文件名称为:" + fileName);
		String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
		if (!"xlsx".equals(prefix)) {
			return new ResultListVO<>(false, "只支持2007版Excel!", null);
		}
		MSystemConfigInfoForm form = new MSystemConfigInfoForm();
		form.setConfig_name(MSystemConstants.CALL_CHARGE_PRODUCT_ID);
		ResultObjectVO<MSysConfigVO> configResult = systemConfigInfoServiceImpl.queryByIdSystemConfig(form);
		if (null == configResult.getObjVo()||null==configResult.getObjVo().getConfig_value()) {
			log.info("系统配置配置话费对用的产品id请配置{}",MSystemConstants.CALL_CHARGE_PRODUCT_ID);
			return new ResultListVO<>("sx.call.charge.product.id.is.null");
		}
		List<Map<String, String>> listMap = PropertiesUtils.proToMaps(MSystemConstants.PRODUCTCALLCHARGE_FILE_PATH);
		ResultExcelVO results = ExcelUtils.fileToExcel(file, listMap.get(0), listMap.get(1));
		if (!results.isSuccess()) {
			return new ResultListVO<>(false, null, "文件头错误");
		}
		return productCallChargeServiceImpl.importCallCharge(results.getListMap(),configResult.getObjVo() );
	}

	@RequestMapping("/callcharge_byid/{id}")
	@ResponseBody
	public ViewAndModel queryBy_viewcallChargeInfo(MProductCallChargeInfoForm form) {
		ViewAndModel model = new ViewAndModel(request, cache);
		// 查询类
		ResultObjectVO result = productCallChargeServiceImpl.queryById(form);
		model.addObject("mproductCallChargeInfoVO", result.getObjVo());
		model.setViewName("productsMgr/phoneRecharge/addPhoneBill");// 修改ftl页面名称
		return model;
	}

	@RequestMapping(value = "callcharge_eidt")
	@ResponseBody
	public ResultBooleanVO add_callChargeInfo(HttpServletRequest request, HttpServletResponse response) {
		if (null == request) {
			return new ResultBooleanVO("sj.m.common.add.error");
		}
		String requestMessage = extract_param_from_request(request);
		if (null == requestMessage || "".equals(requestMessage)) {
			return new ResultBooleanVO("sj.m.common.add.error");
		}
		MProductCallChargeInfoForm form = JackJsonUtil.jsonToJavaBean(requestMessage, MProductCallChargeInfoForm.class);
		if (null == form) {
			return new ResultBooleanVO("sj.m.common.add.error");
		}
		if (0 == form.getId()) {
			return new ResultBooleanVO("sj.m.common.add.error");
		}
		return this.productCallChargeServiceImpl.editCallCharge(form, loginConsumer());
	}

	/**
	 * [ProductCallChargeInfo]产品设置 流程
	 * 
	 * @return
	 */
	@RequestMapping("/callcharge_setting")
	public ViewAndModel settingProductCallChargeInfo() {
		ViewAndModel model = new ViewAndModel(request, cache);
		model.setViewName("productsMgr/phoneRecharge/phoneSetting");// 产品设置ftl页面名称
		ResultListVO<MProvinceVO> list = mSysProvinceServiceImpl.querySysProvince(null);
		MProductCallChargeVO vo = new MProductCallChargeVO();
		vo.setProvince_list(list.getData());
		model.addObject("vo", vo);
		return model;
	}

	@RequestMapping("/callcharge_productList")
	@ResponseBody
	public MSetCallChargeVO queryCallChargeStatus(HttpServletRequest req, HttpServletResponse resp) {
		String requestMessage = extract_param_from_request(req);
		if (null == requestMessage) {
			return new MSetCallChargeVO(false, "设置条件错误", null);
		}
		// ProductCallChargeInfoVO vo 查询条件VO
		MProductCallChargeInfoForm form = JackJsonUtil.jsonToJavaBean(requestMessage, MProductCallChargeInfoForm.class);
		ResultListVO<MProductCallChargeVO> result = this.productCallChargeServiceImpl.queryCallCharge(form);
		// SettingCallChargeInfoVO svo 返回数据 VO
		MSetCallChargeVO svo = new MSetCallChargeVO();
		List<MStatusProductVO> list = new ArrayList<>();
		if (null == result.getData()) {
			return new MSetCallChargeVO(false, "没有要查询的信息", null);
		}
		for (MProductCallChargeVO pvo : result.getData()) {
			// StatusCallChargeInfoVO sv 返回数据状态
			MStatusProductVO sv = new MStatusProductVO();
			sv.setProduct_name(pvo.getProduct_name());
			sv.setStatus(pvo.getStatus());
			list.add(sv);
		}
		svo.setSetting_list(list);
		svo.setProvince_name(form.getProvince_name());
		svo.setMtc_name(form.getMtc_name());
		svo.setSuccess(true);

		return svo;
	}

	/**
	 * [ProductCallChargeInfo]停用启用 流程
	 * 
	 * @return productSave
	 */
	@RequestMapping("/callcharge_edit")
	@ResponseBody
	public ResultBooleanVO changeCallChargeStatus(HttpServletRequest request, HttpServletResponse response) {
		String requestMessage = extract_param_from_request(request);
		if (null == requestMessage) {
			return new ResultBooleanVO(false, "设置条件错误", null);
		}
		log.info("requestMessage:" + requestMessage);

		return productCallChargeServiceImpl.changeCallChargeStatus(requestMessage);
	}

}
